<?php

namespace app\api\controller;


use app\admin\model\Deliverytime;
use app\api\model\Order as OrderModel;
use app\common\controller\Api;
use think\Request;

/**
 * 配送员
 */
class Deliverystaff extends Api
{

    protected $noNeedRight = ['*'];


    protected $deliverystaff_id;
    protected $realname;


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $deliverystaff = \app\admin\model\DeliveryStaff::get(['user_id'=>$this->auth->id,'status'=>1]);
        if (empty($deliverystaff)){
            $this->error('当前用户不是配送员');
        }
        $this->deliverystaff_id = $deliverystaff->id;
        $this->realname = $deliverystaff->realname;
    }

    //筛选
    public function filter()
    {
        $community_ids = \app\admin\model\shop\Order::where(['deliverystaff_id'=>$this->deliverystaff_id,'orderstate'=>0,'shippingstate'=>0])
            ->group('community_id')->column('community_id');

        $community_list = \app\admin\model\Community::where('id','in',$community_ids)
            ->field('id,community_name')->select();

        $date = \app\admin\model\shop\Order::where(['deliverystaff_id'=>$this->deliverystaff_id,'orderstate'=>0,'shippingstate'=>0])
            ->group('date')->column('date');

        $shippingtime = \app\admin\model\shop\Order::where(['deliverystaff_id'=>$this->deliverystaff_id,'orderstate'=>0,'shippingstate'=>0])
            ->group('shippingtime')->column('shippingtime');


        $data = [
            'community_list'=>$community_list,
            'date'=>$date,
            'shippingtime'=>$shippingtime
        ];

        $this->success('',$data);
    }


    //订单
    public function order()
    {
        try {
            $params = input();
            $model = new \app\api\model\Deliverystaff();
            $list = $model->orderlist($params,$this->deliverystaff_id);
            foreach ($list as $item) {
                $item->append(['status_text']);
                $field = 'id,amount,date,saleamount,paystate,orderstate,shippingstate,order_goods,status,order_sn,status_text,expiretime,address,receiver,mobile';
                $item->visible(explode(',', $field));
                $item->expiretime = $item->expiretime - time();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$list);
    }

    //订单详情
    public function orderdetail()
    {
        $order_sn = $this->request->param('order_sn');
        if (empty($order_sn)) {
            $this->error('参数缺失');
        }
        $order = OrderModel::getDeliveryDetail($order_sn, $this->deliverystaff_id);
        if (empty($order)) {
            $this->error('未找到订单');
        }
        $order->append(['status_text']);
        $order->hidden(explode(',', 'method,transactionid,updatetime,deletetime'));
        $this->success('', $order);
    }

    //开始配送
    public function delivery()
    {
        try {
            $order_sn = input('order_sn','');
            $model = new \app\api\model\Deliverystaff();
            $res = $model->delivery($order_sn,$this->deliverystaff_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //批量开始配送
    public function batchdelivery()
    {
        try {
            $order_ids = input('order_ids','');
            $model = new \app\api\model\Deliverystaff();
            $res = $model->batchdelivery($order_ids,$this->deliverystaff_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //确认送达
    public function takedelivery()
    {
        try {
            $order_sn = input('order_sn','');
            $model = new \app\api\model\Deliverystaff();
            $res = $model->takedelivery($order_sn,$this->deliverystaff_id,$this->realname);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //拍照上传
    public function uploadimg()
    {

        try {
            $order_sn = input('order_sn','');
            $remark = input('remark','');
            $images = input('images','');
            $model = new \app\api\model\Deliverystaff();
            $res = $model->uploadimg($order_sn,$remark,$images,$this->deliverystaff_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);

    }

}